package com.dews.web.controller.course;

import com.alibaba.excel.EasyExcelFactory;
import com.dews.common.annotation.Log;
import com.dews.common.core.controller.BaseController;
import com.dews.common.core.domain.BaseResponse;
import com.dews.common.enums.BusinessType;
import com.dews.common.utils.excel.CommonListener;
import com.dews.common.utils.excel.ExcelClient;
import com.dews.common.utils.excel.ExcelSelectHandler;
import com.dews.common.utils.excel.ExcelSelectRo;
import com.dews.common.utils.poi.ExcelUtil;
import com.dews.course.domain.ro.StudentScoreDetailRo;
import com.dews.course.domain.ro.StudentScoreRo;
import com.dews.course.domain.vo.StudentScoreVo;
import com.dews.course.service.IStudentScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 学生课程成绩Controller
 *
 * @author qingfeng
 * @date 2025-01-21
 */
@Api(value = "学生课程成绩", tags = {"学生课程成绩"})
@RestController
@RequestMapping("/course/score")
public class StudentScoreController extends BaseController {

    private IStudentScoreService studentScoreService;

    @Autowired
    public StudentScoreController(IStudentScoreService studentScoreService) {
        this.studentScoreService = studentScoreService;
    }

    @ApiOperation(value = "导出学生成绩导入模板")
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        // 查询封装下拉框需要的内容
        List<ExcelSelectRo> excelSelectRoList = studentScoreService.selectDropDown();
        // 调用方法实现写操作
        ExcelClient.createDynamicExcel("学生成绩导入模板", StudentScoreRo.class, Collections.emptyList(), new ExcelSelectHandler(excelSelectRoList), response);
    }

    @Log(title = "学生课程成绩" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('course:score:import')")
    @PostMapping("/importData")
    public BaseResponse importData(MultipartFile file, @RequestParam(value = "courseCode", required = false) String courseCode) throws Exception {
        ArrayList<StudentScoreRo> studentScoreRoList = new ArrayList<>();
        // 定义接收失败的集合
        ArrayList<StudentScoreRo> errorList = new ArrayList<>();
        // 读取Excel文件内容
        EasyExcelFactory.read(
                        file.getInputStream(),
                        StudentScoreRo.class,
                        new CommonListener<StudentScoreRo>(studentScoreRoList,100, () -> {
                            studentScoreService.importData(studentScoreRoList, errorList, courseCode);
                            return true;
                        })
                )
                .sheet()
                .doRead();

        // TODO 后续需要完善消息通知

        return success();
    }

    @ApiOperation(value = "专业课程点击详情查询课程下的学生成绩信息")
    @PreAuthorize("@ss.hasPermi('course:score:detail')")
    @GetMapping("/detail/{courseId}")
    public BaseResponse scoreDetail(@PathVariable("courseId") Integer courseId) {
        List<StudentScoreDetailRo> list = studentScoreService.selectStudentScoreByCourseId(courseId);
        return success(list);
    }

    @ApiOperation(value = "学生查询课程成绩列表")
    @PreAuthorize("@ss.hasPermi('course:score:stu')")
    @GetMapping("/stuScoreDetail")
    public BaseResponse stuScoreDetail() {
        List<StudentScoreDetailRo> list = studentScoreService.stuScoreDetail();
        return success(list);
    }












    @ApiOperation(value = "导出学生课程成绩列表")
    @PreAuthorize("@ss.hasPermi('course:score:export')")
    @Log(title = "学生课程成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentScoreVo studentScoreVo) {
        List<StudentScoreVo> list = studentScoreService.selectStudentScoreList(studentScoreVo);
        ExcelUtil<StudentScoreVo> util = new ExcelUtil<StudentScoreVo>(StudentScoreVo.class);
        util.exportExcel(response, list, "学生课程成绩数据");
    }

    @ApiOperation(value = "获取学生课程成绩详细信息")
    @PreAuthorize("@ss.hasPermi('course:score:query')")
    @GetMapping(value = "/{id}")
    public BaseResponse getInfo(@PathVariable("id") Integer id) {
        return success(studentScoreService.selectStudentScoreById(id));
    }

    @ApiOperation(value = "新增学生课程成绩")
    @PreAuthorize("@ss.hasPermi('course:score:add')")
    @Log(title = "学生课程成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResponse add(@RequestBody StudentScoreVo studentScoreVo) {
        return toAjax(studentScoreService.insertStudentScore(studentScoreVo));
    }

    @ApiOperation(value = "修改学生课程成绩")
    @PreAuthorize("@ss.hasPermi('course:score:edit')")
    @Log(title = "学生课程成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResponse edit(@RequestBody StudentScoreVo studentScoreVo) {
        return toAjax(studentScoreService.updateStudentScore(studentScoreVo));
    }

    @ApiOperation(value = "删除学生课程成绩")
    @PreAuthorize("@ss.hasPermi('course:score:remove')")
    @Log(title = "学生课程成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public BaseResponse remove(@PathVariable List<Integer> ids) {
        return toAjax(studentScoreService.deleteStudentScoreByIds(ids));
    }


}
